// pages/renewal/renewal.js
const http = require('../../utils/http')
const api = require('../../utils/api')
const util = require('../../utils/util')
const ui = require('../../utils/ui')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    itemId: "",
    tradeId: "",
    startDate: null,
    endDate: null,
    roomData: null, //服务器返回数据
    sliderValue: 1,
    totalPrice: 0,
    totalPriceMember: 0,
  },

  onInput(e) {
    let value = e.detail.value
    if (value == 0.5) {
      ui.showToast({
          title: '温馨提示: 房间续费0.5小时起续',
      })
    }
    const price = this.data.roomData.item.price
    const memberPrice = this.data.roomData.item.memberPrice ?? price
    this.setData({
      sliderValue: value,
      totalPrice: util.keepTwoDecimal(parseFloat(price) * parseFloat(value)),
      totalPriceMember: util.keepTwoDecimal(parseFloat(memberPrice) * parseFloat(value)),
    })
  },

  requestData() {
    if (this.data.date == null) {
      this.setData({
        date: new Date(new Date().setHours(0, 0, 0, 0)),
      })
    }
    let dt = this.data.date
    let year  = dt.getFullYear();
    let month = (dt.getMonth() + 1).toString().padStart(2, "0");
    let day = dt.getDate().toString().padStart(2, "0");
    let dateStr = year+month+day
    http.request({
      showLoading: true,
      url: api.occupied,
      data: {
        date: dateStr,
        memberId: getApp().globalData.memberId,
        itemId: this.data.itemId,
      }
    }).then(res=>{
      //处理轮播数据
      let data = res.data.result
      if (data.item.pictures == null) {
        data.item.pictures = "https://upload-images.jianshu.io/upload_images/5809200-a99419bb94924e6d.jpg"
      }
      let images = data.item.pictures.split(',')
      data.item.pictures = images

      const price = data.item.price
      const memberPrice = data.item.memberPrice ?? price
      this.setData({
        roomData: data,
        totalPrice: util.keepTwoDecimal(parseFloat(price) * parseFloat(this.data.sliderValue)),
        totalPriceMember: util.keepTwoDecimal(parseFloat(memberPrice) * parseFloat(this.data.sliderValue)),
      })
    }).catch(err=>{
      console.log(err)
    });
  },

  _preview(event) {
    let currentUrl = event.currentTarget.dataset.src
    wx.previewImage({
      current: currentUrl,
      urls: this.data.roomData.item.pictures.length > 0 ? this.data.roomData.item.pictures : [currentUrl]
    })
  },

  submit: function (event) {
    const durationTime = parseFloat(this.data.sliderValue)

    if(durationTime < 0.5){
      wx.showToast({
        title: '时长至少为0.5小时',
        icon: 'none',
        duration: 1500,
        mask:true
        })
        return;
    }
    const startDate = util.formatTimeFromStamp(this.data.startDate)
    const hour = this.data.endDate.getHours()
    const min = this.data.endDate.getMinutes()
    const endDate = util.formatTimeFromStamp(new Date(this.data.endDate).setHours(hour, min+parseFloat(this.data.sliderValue*60), 0, 0))

    let totalPrice = this.data.totalPrice
    // if (this.data.roomData.isMember) {
    //   totalPrice = this.data.totalPriceMember
    // }

    http.request({
      url: api.renewal,
      method: "POST",
      data: {
        durationTime: durationTime,
        totalPrice: totalPrice,
        tradeId: this.data.tradeId,
      }
    }).then(res=>{
      console.log(res)
      let data = res.data.result
      if (data.state) {
        let params = {
          "isRenewal": true,
          "oid": data.tradeId,
          "name": this.data.roomData.item.itemName,
          "desc": this.data.roomData.item.describes,
          "url": encodeURIComponent(this.data.roomData.item.itemLogo),
          "time": startDate+" 至 "+endDate, 
          "duration": durationTime, 
          "price": this.data.totalPrice,
          "memberPrice":  this.data.totalPriceMember
        } 
        this.toOrderDetail(params)
      }
      else {
        ui.showToast({
          title: res.data.result.msg,
        })
      }
      
    })
  },

  toOrderDetail(params) {  
    wx.navigateTo({
      url: '/pages/order_detail/order_detail'+util.paramsWrap(params),
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      itemId: options.itemId,
      tradeId: options.tradeId,
      startDate: new Date(options.startDate),
      endDate: new Date(options.endDate),
    })
    this.requestData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})